import chalk from 'chalk';
import {LogLevel} from "../types";

/**
 * 日志工具类，提供不同级别的日志输出功能
 */
export class Logger {
  private static instance: Logger;
  private static logLevel: LogLevel = 'info';

  private constructor() {
  }

  /**
   * 获取Logger单例
   */
  public static getInstance(): Logger {
    if (!Logger.instance) {
      Logger.instance = new Logger();
    }
    return Logger.instance;
  }

  /**
   * 设置日志级别
   */
  public static setLogLevel(level: LogLevel): void {
    Logger.logLevel = level;
  }

  /**
   * 获取当前日志级别
   */
  public static getLogLevel(): LogLevel {
    return Logger.logLevel;
  }

  /**
   * 获取时间戳字符串
   */
  private getTimeStamp(): string {
    return new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
  }

  /**
   * 调试日志
   */
  public debug(message: string): void {
    if (['debug'].includes(Logger.logLevel)) {
      console.log(`${chalk.gray(this.getTimeStamp())} ${chalk.blue('[DEBUG]')} ${message}`);
    }
  }

  /**
   * 信息日志
   */
  public info(message: string): void {
    if (['debug', 'info'].includes(Logger.logLevel)) {
      console.log(`${chalk.gray(this.getTimeStamp())} ${chalk.green('[INFO]')} ${message}`);
    }
  }

  /**
   * 警告日志
   */
  public warn(message: string): void {
    if (['debug', 'info', 'warn'].includes(Logger.logLevel)) {
      console.log(`${chalk.gray(this.getTimeStamp())} ${chalk.yellow('[WARN]')} ${message}`);
    }
  }

  /**
   * 错误日志
   */
  public error(message: string): void {
    if (['debug', 'info', 'warn', 'error'].includes(Logger.logLevel)) {
      console.error(`${chalk.gray(this.getTimeStamp())} ${chalk.red('[ERROR]')} ${message}`);
    }
  }
}
